import axios from "axios";
import storage from "./index.js";
//区分开发环境和生产环境的请求地址
import config from "../config/index.js";
import  router  from "../router/index.js";

// const  PARAM_ERROR='参数不正确'
// const USER_ACCOUNT_ERROR='用户账号密码错误'
// const USER_LOGIN_ERROR="用户未登录"
// const BUSINESS_ERROR='网络请求失败'
// const AUTH_ERROR='认证失败或TOKEN过期'
const http=axios.create({
    baseURL:config.baseUrl,
    timeout:500  
})
//axios拦截器 对返回的结果进行处理
http.interceptors.response.use(function (res) {
  const {code,data,msg}=res.data

   
  if (code!=200) {
    console.log(res,code);
    if (code==500001) {
      router.push("/login")
      
    }else{
      return Promise.reject(res,code);
    }
    
  }else if(code==200){
   return  Promise.resolve(res,code)
  }
 
})

const requestList = ["get", "post"]

requestList.forEach(item => {
    request[item] = (url, data, options) => {
        return request({
            url,
            data,
            method: item,
            ...options,
        })
    }
})

  // }, function (error) {
  //   console.log(error.request);
  //   // return Promise.reject(error);
  //   if (error.request.status==401) {
     
      
  //      setTimeout(() => {
  //       router.push("/allLogin")
  //      }, 1000);
  //   }
  //   return error;
  // });

  http.interceptors.request.use(function (config) {
          
    let result=storage.getSession("userInfo")
     
      if (result&&result!={}) {
      
       config.headers.Authorization='Bearer '+result.data.token
     }
    
     return config

  }, function (error) {
    console.log(error);
   
    return error.response;
  });


export function request(options){
   options.method=options.method||'get'
    if (options.method=='get') {
      options.params=options.data
    }
    return http(options)
}
export const post=(url,data)=>{
    return new Promise((resolve,reject)=>{
        http.post(url,data,{
            headers:{
                "Content-type": "application/json",
            }
        }
        ).then((res)=>{
          
            resolve(res);
        },err=>{
          reject(err)
        })
      
    })
}


export const get=(url,data)=>{
  return new Promise((resolve,reject)=>{
      http.get(url,data,{
          headers:{
              "Content-type": "application/json",
          }
      }

      ).then((res)=>{
         
          resolve(res);
      },err=>{
        reject(err)
      })
    
  })
}
export const deleteService=(url,data)=>{
  return new Promise((resolve,reject)=>{
      http.delete(url,{
          headers:{
              "Content-type": "application/json",
          },
          params:data
      }

      ).then((res)=>{
         
          resolve(res);
      },err=>{
        reject(err)
      })
    
  })
}
export const getData=(url,data)=>{
    return new Promise((resolve,reject)=>{
        http.get(url,{
            headers:{
                "Content-type": "application/json",
            },
            params:data
}
  
        ).then((res)=>{
           
            resolve(res);
        },err=>{
          reject(err)
        })
      
    })
  }
// 更改接口
export const put=(url,data)=>{
  return new Promise((resolve,reject)=>{
      http.put(url,data,{
          headers:{
              "Content-type": "application/json",
          }
      }

      ).then((res)=>{
          
          resolve(res);
      },err=>{
        reject(err)
      })
    
  })
}
export const otherget = (url, params = {}) => {
     return new Promise((resolve, reject) => {
    http.get(url, { params }).then(
      (response) => {
        resolve(response.data);
      },
      (err) => {
        reject(err);
      }
    );
  });
  
  };
//上传用户头像
export const postFile = (url, data) => {
  return new Promise((resolve, reject) => {
      http.post(url, data, {
          headers: {
              'Content-Type': 'multipart/form-data'
          }
      })
          .then(res => {
              resolve(res);
          })
          .catch(err => {
              reject(err);
          });
  });
};
// 删除接口
export const deleteData =(url,data)=>{
  return new Promise((resolve, reject) => {
    http.delete(url, data, {
        headers: {
          "Content-type": "application/json",
        }
    })
        .then(res => {
            resolve(res);
        })
        .catch(err => {
            reject(err);
        });
});
}